package com.shop.cloud.controller.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.City;
import com.shop.cloud.domain.hongfu.Province;
import com.shop.cloud.domain.hongfu.Region;
import com.shop.cloud.domain.hongfu.SysConfig;
import com.shop.cloud.model.request.GetAreaListRequest;
import com.shop.cloud.model.request.GetConfigRequest;
import com.shop.cloud.model.request.SetConfigRequest;
import com.shop.cloud.model.response.GetAreaListResponse;
import com.shop.cloud.model.response.GetConfigResponse;
import com.shop.cloud.model.response.SetConfigResponse;
import com.shop.cloud.service.hongfu.impl.CityService;
import com.shop.cloud.service.hongfu.impl.ProvinceService;
import com.shop.cloud.service.hongfu.impl.RegionService;
import com.shop.cloud.service.hongfu.impl.SysConfigService;
import com.shop.cloud.util.Response;
import com.shop.cloud.util.ResponseUtil;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "common")
public class CommonController extends BaseController {

	private static final Logger logger = Logger.getLogger(CommonController.class);

	@Autowired
	private ProvinceService provinceService;
	
	@Autowired
	private CityService cityService;
	
	@Autowired
	private RegionService regionService;
	
	@Autowired
	private SysConfigService sysConfigService;

	//@MagicPermission
	@ApiOperation(value = "获取地区列表", notes = "获取地区列表")
	@RequestMapping(value = "arealist", method = RequestMethod.POST)
	@ResponseBody
	public Response areaList(HttpServletRequest request, HttpServletResponse response,@RequestBody GetAreaListRequest getAreaListRequest) {
		List<GetAreaListResponse> datas = new ArrayList<GetAreaListResponse>();
		GetAreaListResponse data = null;

		logger.debug("获取地区列表");
		
		long flag = getAreaListRequest.getType();//类型。1省份，2城市，3区域
		
		if (flag == 1) {
			Province province = new Province();
			
			List<Province> list = provinceService.find(province);
			for (Province p : list) {
				data = new GetAreaListResponse();
				data.setId(p.getId());
				data.setName(p.getName());
				
				datas.add(data);
			}
		}
		
		if (flag == 2) {
			City city = new City();
			city.setProvinceId(getAreaListRequest.getParentId());
			
			List<City> list = cityService.find(city);
			for (City c : list) {
				data = new GetAreaListResponse();
				data.setId(c.getId());
				data.setName(c.getName());
				
				datas.add(data);
			}
		}
		
		if (flag == 3) {
			Region region = new Region();
			region.setCityId(getAreaListRequest.getParentId());
			
			List<Region> list = regionService.find(region);
			for (Region r : list) {
				data = new GetAreaListResponse();
				data.setId(r.getId());
				data.setName(r.getName());
				
				datas.add(data);
			}
		}
		
		return ResponseUtil.ok(datas);
	}
	
	//@MagicPermission
	@ApiOperation(value = "获取配置字段值", notes = "获取配置字段值")
	@RequestMapping(value = "getConfig", method = RequestMethod.POST)
	@ResponseBody
	public Response getConfig(HttpServletRequest request, HttpServletResponse response,@RequestBody GetConfigRequest getConfigRequest) {
		GetConfigResponse data = new GetConfigResponse();
		SysConfig sysConfig = getSysConfig(getConfigRequest.getField());
		
		if(sysConfig != null) 
			data.setValue(sysConfig.getValue());
		else 
			data.setValue("");
		
		return ResponseUtil.ok(data);
	}
	
	//@MagicPermission
	@ApiOperation(value = "设置配置字段值", notes = "设置配置字段值")
	@RequestMapping(value = "setConfig", method = RequestMethod.POST)
	@ResponseBody
	public Response setConfig(HttpServletRequest request, HttpServletResponse response,@RequestBody SetConfigRequest setConfigRequest) {
		SetConfigResponse data = new SetConfigResponse();
		
		setSysConfig(setConfigRequest.getField(), setConfigRequest.getValue());
		
		return ResponseUtil.ok(data);
	}
	
	private SysConfig getSysConfig(String field) {
		SysConfig sysConfig = null;
		Map<String, Object> params = new HashMap<String, Object>();
		params = new HashMap<String, Object>();
	 	params.put("field", field);
	 	sysConfig = sysConfigService.findOne(params);
		return sysConfig;
	}
	
	private SysConfig setSysConfig(String field, String value) {
		SysConfig sysConfig = null;
		Map<String, Object> params = new HashMap<String, Object>();
		params = new HashMap<String, Object>();
	 	params.put("field", field);
	 	sysConfig = sysConfigService.findOne(params);
	 	if(sysConfig != null) {
	 		sysConfig.setValue(value);
	 		sysConfigService.update(sysConfig);
	 	} else {
	 		sysConfig = new SysConfig();
	 		sysConfig.setType(2);
	 		sysConfig.setField(field);
	 		sysConfig.setValue(value);
	 		sysConfigService.insert(sysConfig);
	 	}
		return sysConfig;
	}
}
